#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusReply>
#include <QDir>
#include <QDebug>
#include "common.h"
#include "DeepinMonitor.h"

QDir Platform::dataDir = QDir(QDir::homePath() + "/.local/share/TranslateStar");

IMonitor& Platform::monitor()
{
    return DeepinMonitor::instance();
}


static QString getActiveWindow()
{
    QDBusMessage msg = QDBusMessage::createMethodCall("org.ayatana.bamf",
                                                      "/org/ayatana/bamf/matcher",
                                                      "org.ayatana.bamf.matcher",
                                                      "ActiveWindow");

    QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
    if (reply.type() == QDBusMessage::ReplyMessage)
        return reply.arguments()[0].toString();
    else
        return "";
}


unsigned long Platform::getActiveWindowPID()
{
    QString windowPath = getActiveWindow();
    if (!windowPath.isEmpty())
    {
        QDBusMessage msg = QDBusMessage::createMethodCall("org.ayatana.bamf",
                                                          windowPath,
                                                          "org.ayatana.bamf.window",
                                                          "GetPid");

        QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
        if (reply.type() == QDBusMessage::ReplyMessage)
            return reply.arguments()[0].toUInt();
    }
    return 0;
}

QString Platform::getActiveWindowName()
{
    QString windowPath = getActiveWindow();
    if (!windowPath.isEmpty())
    {
        QDBusMessage msg = QDBusMessage::createMethodCall("org.ayatana.bamf",
                                                          windowPath,
                                                          "org.ayatana.bamf.view",
                                                          "Name");

        QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
        if (reply.type() == QDBusMessage::ReplyMessage)
            return reply.arguments()[0].toString();
    }
    return "";
}

QString Platform::getActiveApplicationName()
{
    QDBusMessage msg = QDBusMessage::createMethodCall("org.ayatana.bamf",
                                                      "/org/ayatana/bamf/matcher",
                                                      "org.ayatana.bamf.matcher",
                                                      "ActiveApplication");

    QDBusMessage reply = QDBusConnection::sessionBus().call(msg);
    if (reply.type() != QDBusMessage::ReplyMessage)
        return "";

    QString appPath = reply.arguments()[0].toString();
    msg = QDBusMessage::createMethodCall("org.ayatana.bamf",
                                         appPath,
                                         "org.ayatana.bamf.view",
                                         "Name");

    reply = QDBusConnection::sessionBus().call(msg);
    if (reply.type() == QDBusMessage::ReplyMessage)
        return reply.arguments()[0].toString();
    else
        return "";
}
